# -*- coding: utf-8 -*-
# @Time    : 2021/11/17 16:10
# @Author  : CHENlj
# @File    : shake.py
# @Software: PyCharm
"""
摇一摇概率验证
"""
import requests, json

token = '202824e3ed8b94c7387efd11fd4b51e7'
host = 'http://8.134.36.205:8201'
shake_path = '/redPacket/shake'  # 摇一摇获取奖励
award_path = '/redPacket/getAward'  # 领取奖励

headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Token": token
}
num = 100  # 摇的次数
money_count = 0
coin_count = 0
shake_num = 0
for i in range(0, num):
    shake_res = requests.post(host+shake_path, headers=headers)
    try:
        key = shake_res.json()['DATA']['award_info']['key']
        if i == num - 1:
            shake_num = shake_res.json()['DATA']['shake_num']
    except Exception:
        key = None
    data = {
        "key": key
    }
    data = json.dumps(data)
    if key:
        award_res = requests.post(host+award_path, data, headers=headers)
        try:
            if award_res.json()['DATA']['type'] == 1:
                coin_count += award_res.json()['DATA']['val']
                print('摇到现金币：%d 个' % award_res.json()['DATA']['val'])
            if award_res.json()['DATA']['type'] == 2:
                money_count += award_res.json()['DATA']['val']
                print('摇到现金：%d 分' % award_res.json()['DATA']['val'])
            if i == 0:
                file_handle = open('award_data.txt', mode='w', encoding='gbk')
            else:
                file_handle = open('award_data.txt', mode='a', encoding='gbk')
            file_handle.write(award_res.json() + '\n')
            file_handle.close()
        except Exception:
            if i == 0:
                file_handle = open('award_data.txt', mode='w', encoding='gbk')
            else:
                file_handle = open('award_data.txt', mode='a', encoding='gbk')
            file_handle.write(award_res.text + '\n')
            file_handle.close()
    else:
        print(shake_res.text)
print('='*30)
print('共摇到现金：%d 分' % money_count)
print('共摇到现金币：%d 个' % coin_count)
print('今天共摇了：%d 次' % shake_num)


